import { mapIcon1, mapIcon2, mapIcon3, mapIcon4, mapIcon7 } from '/@/components/chinaMap/chinaHooks.ts';
import { watchIcon } from '/@/components/secondaryScreen/secondMap/secondMapHooks.ts';
import * as turf from '@turf/turf';

/**
 * @desc 创建icon
 * */
export function createIcon({ icon, iconSize, imageSize }) {
    return new AMap.Icon({
        // 图标尺寸
        size: new AMap.Size(...iconSize),
        // 图标的取图地址
        image: icon,
        // 图标所用图片大小
        imageSize: new AMap.Size(...imageSize),
    });
}

/**
 * @desc 根据类型创建Icon
 * @type 1:油田医院，2:合作医院，3:医疗点
 */
export function typeToIcon(type: string, centerResource: boolean) {
    const iconSize = [18, 44];
    const imageSize = [18, 44];
    if (type == '1') {
        return createIcon({ icon: mapIcon1, iconSize, imageSize });
    }
    if (type == '2') {
        return createIcon({ icon: mapIcon4, iconSize, imageSize });
    }
    //关联医疗点
    if (type == '3' && centerResource) {
        return createIcon({ icon: mapIcon7, iconSize, imageSize });
    }
    if (type == '3') {
        return createIcon({ icon: mapIcon2, iconSize, imageSize });
    }
    if (type == '5' || type == '4') {
        return createIcon({ icon: mapIcon3, iconSize, imageSize });
    }
    if (type == '6') {
        return createIcon({ icon: watchIcon, iconSize: [23, 51], imageSize: [23, 51] });
    }
}

/**
 *
 */
export function createCircle(option) {
    const { lon, lat } = option;
    return new AMap.Circle({
        center: [lon, lat],
        radius: 100000, //半径
        borderWeight: 3,
        strokeColor: '#FF33FF',
        // strokeWeight: 6,
        strokeOpacity: 0.2,
        fillOpacity: 0.4,
        strokeStyle: 'dashed',
        strokeDasharray: [10, 10],
        // 线样式还支持 'dashed'
        fillColor: '#1791fc',
        zIndex: 50,
        ...option,
    });
}

export function getLonLat(result = []) {
    let arr = [];
    for (let i = 0; i < result.length; i++) {
        let { lon, lat, lng } = result[i];
        if (lng) {
            lon = lng;
        }
        if (!lon || !lat) {
            continue;
        }
        let point = turf.point([lon, lat]);
        arr.push(point);
    }
    return arr;
}

/**
 * @desc 根据多点算中心点
 */
export function getCenter(list = []) {
    if (!list || !Array.isArray(list)) {
        return undefined;
    }
    if (list.length < 1) {
        return undefined;
    }
    let features = turf.featureCollection(list);
    let centerObj = turf.center(features);
    return centerObj?.geometry?.coordinates;
}
